package com.qianli.user.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.fqgj.xjd.user.entity.UserCreditCardBillEntity;
import com.fqgj.xjd.user.mapper.UserCreditCardBillMapper;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * name:信用卡账单过期job
 * auth:ting.li@qianli-inc.com
 * date:2018/1/19 10:50
 * version: v1.0.0
 **/
public class ExpiredCreditCardBillJob implements SimpleJob{

    private static final Logger LOGGER = LoggerFactory.getLogger(ExpiredCreditCardBillJob.class);

    private static final int PAGE_SIZE = 500;
    @Autowired
    private UserCreditCardBillMapper userCreditCardBillMapper;
    @Override
    public void execute(ShardingContext shardingContext) {
        long startTime = System.currentTimeMillis();
        LOGGER.info("expired creditcard bill job start...");
        List<UserCreditCardBillEntity> entities = null;
        int start = 0;
        while (true){
            entities = userCreditCardBillMapper.pagedQueryExpiredCreditCardBills(start,PAGE_SIZE);
            if(CollectionUtils.isNotEmpty(entities)){
                break;
            }
            start++;
            //设置过期
            for(UserCreditCardBillEntity entity:entities){
                entity.setExpired(1);
                userCreditCardBillMapper.updateByPrimaryKey(entity);
            }
        }
        LOGGER.info("expired creditcard bill job end...,all cost:" + (System.currentTimeMillis()-startTime));
    }
}
